package com.atXiao.food.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atXiao.food.config.Log;
import com.atXiao.food.constant.BusSignConstant;
import com.atXiao.food.constant.BuscodeConstant;
import com.atXiao.food.dao.RecipeDao;
import com.atXiao.food.dao.UserHistoryDao;
import com.atXiao.food.entity.RecipeEntity;
import com.atXiao.food.entity.UserhistoryEntity;
import com.atXiao.food.reqbody.receipt.ReceiptDetailReq;
import com.atXiao.food.reqbody.receipt.ReceiptIconReq;
import com.atXiao.food.rspbody.recipe.ReceiptDetail;
import com.atXiao.food.rspbody.recipe.ReceiptIcon;
import com.atXiao.food.service.ReceiptService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: lixiaoxiao
 * @date: 2023/11/23 14:16
 * @version: 1.0
 * @desc:
 */
@Service
public class ReceiptServiceImpl implements ReceiptService {

    public final static Log logger = new Log(ReceiptServiceImpl.class);

    @Autowired
    RecipeDao recipeDao;

    @Autowired
    UserHistoryDao userHistoryDao;

    @Override
    public List<ReceiptIcon> queryReceiptIconList(String trace, ReceiptIconReq iconReq) {
        // 分类编号
        Integer categoryId = iconReq.getCategoryId();
        QueryWrapper<RecipeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        // 查询数据库菜谱列表数据
        List<RecipeEntity> recipeEntityList = recipeDao.selectList(queryWrapper);
        List<ReceiptIcon> iconList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(recipeEntityList)) {
            // 数据组装
            iconList = recipeEntityList.stream().map(item -> {
                ReceiptIcon iconItem = new ReceiptIcon();
                BeanUtils.copyProperties(item, iconItem);
                return iconItem;
            }).collect(Collectors.toList());
        }
        logger.info(trace, BusSignConstant.TASTE_FOOD, BuscodeConstant.CATEGORY_ICON_QUERY, "icon列表查询数据:" + JSONObject.toJSONString(recipeEntityList));
        return iconList;
    }

    @Override
    public ReceiptDetail queryReceiptDetailById(String trace, ReceiptDetailReq detailReq) {
        // 菜谱编号
        String receiptId = detailReq.getReceiptId();
        // 用户编号
        String userId = detailReq.getUserId();
        // 查询详情
        QueryWrapper<RecipeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("recipe_id", receiptId);
        RecipeEntity recipeEntity = recipeDao.selectOne(queryWrapper);
        ReceiptDetail receiptDetail = new ReceiptDetail();
        if (!ObjectUtils.isEmpty(recipeEntity)) {
            BeanUtils.copyProperties(recipeEntity, receiptDetail);
            // 记录用户查看的详情记录
            UserhistoryEntity userhistoryEntity = new UserhistoryEntity();
            userhistoryEntity.setUserId(userId);
            userhistoryEntity.setBroseTime(new Date());
            userhistoryEntity.setRecipeId(receiptId);
            // 写入浏览记录
            userHistoryDao.insert(userhistoryEntity);
            logger.info(trace,BusSignConstant.TASTE_FOOD,BuscodeConstant.CATEGORY_DETAIL_QUERY,"用户浏览记录写入成功");
        }
        logger.info(trace, BusSignConstant.TASTE_FOOD, BuscodeConstant.CATEGORY_DETAIL_QUERY, "菜谱详情数据返回:" + JSONObject.toJSONString(receiptDetail));
        return receiptDetail;
    }
}
